package cq.zhcqk.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cq.zhcqk.R;
import cq.zhcqk.activity.MineActivity;
import cq.zhcqk.adapter.NewsPageFragmentAdapter;
import cq.zhcqk.db.ChannelDb;
import cq.zhcqk.entity.Channel;
import cq.zhcqk.entity.MenuPopwindowBean;
import cq.zhcqk.fragment._3_zw._1_ZwgkFragment;
import cq.zhcqk.fragment._3_zw._2_GzyyFragment;
import cq.zhcqk.fragment._3_zw._3_MsbzFragment;
import cq.zhcqk.util.zxing.activity.CaptureActivity;
import cq.zhcqk.widget.CustomViewPager;
import cq.zhcqk.widget.MenuPopwindow;
import cq.zhcqk.widget.NestRadioGroup;

public class _3_ZwFragment extends Fragment implements View.OnClickListener{

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}
	private View view=null;
	private NestRadioGroup rgChannel=null;
	private RadioButton btn1 = null;
	private RadioButton btn2 = null;
	private RadioButton btn3 = null;
	private CustomViewPager viewPager;
	private Context context;
	private View line1,line2,line3,line4;

	private ImageView headerChehua;
	private ImageView headerSearch;
	private TextView headerTitle;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		context = getActivity();
		if(view==null){
			view=inflater.inflate(R.layout.fragment_zw_layout, null);
			rgChannel=(NestRadioGroup)view.findViewById(R.id.rgChannel);
			btn1=(RadioButton)view.findViewById(R.id.btn1);
			btn2=(RadioButton)view.findViewById(R.id.btn2);
			btn3=(RadioButton)view.findViewById(R.id.btn3);
			line1 = view.findViewById(R.id.line1);
			line2 = view.findViewById(R.id.line2);
			line3 = view.findViewById(R.id.line3);
			viewPager=(CustomViewPager)view.findViewById(R.id.vpNewsList_3);
			viewPager.setScanScroll(false);
			rgChannel.setOnCheckedChangeListener(
					new NestRadioGroup.OnCheckedChangeListener() {
						@Override
						public void onCheckedChanged(NestRadioGroup group,
													 int checkedId) {
							setTab();
							if(btn1.getId() == checkedId){
								btn1.setTextColor(getResources().getColor(R.color.read));
								line1.setVisibility(View.VISIBLE);
								viewPager.setCurrentItem(0);
							}else if(btn2.getId() == checkedId){
								btn2.setTextColor(getResources().getColor(R.color.read));
								line2.setVisibility(View.VISIBLE);
								viewPager.setCurrentItem(1);
							}else if(btn3.getId() == checkedId){
								btn3.setTextColor(getResources().getColor(R.color.read));
								line3.setVisibility(View.VISIBLE);
								viewPager.setCurrentItem(2);
							}
						}
					});
			initTab(inflater);
			initViewPager();
			setHeader();
		}
		ViewGroup parent=(ViewGroup)view.getParent();
		if(parent!=null){
			parent.removeView(view);
		}
		return view;
	}
	private List<Fragment> newsChannelList=new ArrayList<Fragment>();
	private NewsPageFragmentAdapter adapter;
	private void initViewPager(){
		newsChannelList.add(new _1_ZwgkFragment());
		newsChannelList.add(new _2_GzyyFragment());
		newsChannelList.add(new _3_MsbzFragment());
		adapter=new NewsPageFragmentAdapter(super.getChildFragmentManager(), newsChannelList);
		viewPager.setAdapter(adapter);
		viewPager.setOffscreenPageLimit(2);
		viewPager.setCurrentItem(0);
	}
	private void initTab(LayoutInflater inflater){
		btn1.setTextColor(getResources().getColor(R.color.read));
		line1.setVisibility(View.VISIBLE);
	}
	@Override
	public void setArguments(Bundle args) {
		// TODO Auto-generated method stub
		super.setArguments(args);
	}
	private void setTab(){
		btn1.setTextColor(getResources().getColor(R.color.gray_text));
		btn2.setTextColor(getResources().getColor(R.color.gray_text));
		btn3.setTextColor(getResources().getColor(R.color.gray_text));
		line1.setVisibility(View.GONE);
		line2.setVisibility(View.GONE);
		line3.setVisibility(View.GONE);
	}
	private void setHeader(){
		headerChehua = (ImageView) view.findViewById(R.id.header_cehua);
		headerSearch = (ImageView) view.findViewById(R.id.header_search);
		headerTitle = (TextView) view.findViewById(R.id.header_title);
		headerChehua.setOnClickListener(this);
		headerSearch.setOnClickListener(this);

		headerChehua.setVisibility(View.VISIBLE);
		headerTitle.setText("政务");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.header_cehua:
//				startActivity(new Intent(context,MineActivity.class));
				initPp();
				break;
		}
	}

	private void initPp(){
		int[] icons = {R.mipmap.scan, R.mipmap.mine};
		String[] texts = {"扫一扫    ", "个人中心"};
		List<MenuPopwindowBean> list = new ArrayList<>();
		MenuPopwindowBean bean = null;
		for (int i = 0; i < icons.length; i++) {
			bean = new MenuPopwindowBean();
			bean.setIcons(icons[i]);
			bean.setTexts(texts[i]);
			list.add(bean);
		}
		final MenuPopwindow pw = new MenuPopwindow(context, list);
		pw.setOnItemClick(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = null;
				if (position == 0){
					startActivityForResult(new Intent(context, CaptureActivity.class),1);
					pw.dismiss();
				}else if(position == 1){
					startActivity(new Intent(context,MineActivity.class));
					pw.dismiss();
				}else {

				}
			}
		});
		pw.showPopupWindow(view.findViewById(R.id.header_cehua));//点击右上角的那个button
	}
}
